package polymonkey.time;

import processing.core.*;

/**
 * 
 * Used for counting delta time (The time in seconds taken by the previous frame). This is the backbone of the Time library. Everything other class will need an instance of this class in the sketch.
 * deltaTime is affected by the timeScale. Changing the value of timeScale results in the ability to slow or speed up time. 
 * 
 * @author Matt Ditton 
 * @version 1 
 */
public class Time
{

	private static long currentTime;
	private static long previousTime;
	private static double deltaTime;
	private static double countTime;	
	private float timeScale = 1.0f;

	PApplet parent;

	public Time(PApplet _parent)
	{

		parent = _parent;

		parent.registerPre(this);

		previousTime = currentTime;

		currentTime = System.currentTimeMillis();
		
		deltaTime = 0;
		
		countTime = 0;
	}

	/**
	 * Called by the PApplet before draw(). 
	 * NOTE: This method is automatic and should not be called by the user. 
	 */
	public void pre()
	{

		previousTime = currentTime;

		currentTime = System.currentTimeMillis(); 
		
		// remember kids: time is counted as a long with 1000 to a second (millisecond)
		// hense the 0.001f to get the float value of deltaTime.
		deltaTime = (currentTime - previousTime) * 0.001d * timeScale;
		
		countTime += deltaTime;

	}
	
	/**
	 * The method for getting time delta. The float is the length of time in seconds that the previous frame took.
	 * 
	 * @return the length of time in seconds for the previous frame
	 */
	public static float getDeltaTime() 
	{
		return (float)deltaTime;
	}

	/**
	 * @return the current scale of time. 
	 */
	public float getTimeScale() {
		return timeScale;
	}

	/**
	 * Sets the current time scale. By default this value is 1.0 making 1 second per second. 0.5 is half a second per second. 2 is 2 seconds per second ect. 
	 * This method is used to slow down or speed up time.
	 * @param timeScale the timeScale to set
	 */
	public void setTimeScale(float timeScale) {
		this.timeScale = timeScale;
	}

	/**
	 * @return The elapsed time since initializing the Time object.
	 */
	public static float getCountTime() {
		return (float)countTime;
	}
	
	/**
	 * Resets Time to be 0
	 */
	
	public void resetTime(){
		
	}
}